﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Product;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Product
{
    public class ItemServices : IItemServices
    {

        public ItemServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(Item model, List<ProductBom> list)
        {

            var result = await Db.InsertIdentityTranAsync(model,list);
            return result;
        }


        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete Item where Id in @Ids;delete ProductBOM where ItemId in @Ids;";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Item where Id = @Id;delete ProductBOM where ItemId =@Id;";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<Item>> GetAllAsync()
        {
            string sql = "select * from Item";
            var list = await Db.QueryAllAsync<Item>(sql);
            return list;
        }


      



        public async Task<IEnumerable<Item>> GetAsync(int PageIndex, int PageSize, string ItemCode, string ItemName, int ItemTypeId)
        {
            string sql = "select * from Item where 1=1 ";

            if (!string.IsNullOrEmpty(ItemCode))
            {
                sql += " and ItemCode like @ItemCode ";
            }

            if (!string.IsNullOrEmpty(ItemName))
            {
                sql += " and ItemName like @ItemName ";
            }


            if (ItemTypeId != -1)
            {
                sql += " and ItemTypeId  = @ItemTypeId ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<Item>(sql, new { PageIndex, PageSize, ItemCode = $"%{ItemCode}%", ItemName = $"%{ItemName}%",ItemTypeId });
            return list;
        }
        
        
        public async Task<IEnumerable<ItemDto>> GetItemStockAsync(int PageIndex, int PageSize, string ItemCode, string ItemName, int ItemTypeId, int WareHouseId)
        {
            string sql = @"select it.*,isk.SalesCount,isk.StockCount,(isk.StockCount-isk.SalesCount) as CurrCount from Item it
            inner join ItemStock isk on isk.ItemId = it.Id and WareHouseId=@WareHouseId where 1=1";

            if (!string.IsNullOrEmpty(ItemCode))
            {
                sql += " and it.ItemCode like @ItemCode ";
            }

            if (!string.IsNullOrEmpty(ItemName))
            {
                sql += " and it.ItemName like @ItemName ";
            }


            if (ItemTypeId != -1)
            {
                sql += " and it.ItemTypeId  = @ItemTypeId ";
            }

            sql += " order by it.CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<ItemDto>(sql, new { PageIndex, PageSize, ItemCode = $"%{ItemCode}%", ItemName = $"%{ItemName}%",ItemTypeId,WareHouseId });
            return list;
        }

        public async Task<int> GetItemStockCountAsync(string ItemCode, string ItemName, int ItemTypeId, int WareHouseId)
        {
            string sql = "select count(*) from Item it inner join ItemStock isk on isk.ItemId = it.Id and WareHouseId=@WareHouseId where 1=1";
            if (!string.IsNullOrEmpty(ItemCode))
            {
                sql += " and ItemCode like @ItemCode ";
            }

            if (!string.IsNullOrEmpty(ItemName))
            {
                sql += " and ItemName like @ItemName ";
            }
            if (ItemTypeId != -1)
            {
                sql += " and ItemTypeId  = @ItemTypeId ";
            }


            var count = await Db.GetRecoreCountAsync(sql, new { ItemCode = $"%{ItemCode}%", ItemName = $"%{ItemName}%", ItemTypeId, WareHouseId });
            return count;
        }




        public async Task<int> GetCountAsync(string ItemCode, string ItemName, int ItemTypeId)
        {
            string sql = "select count(*) from Item where 1=1 ";
            if (!string.IsNullOrEmpty(ItemCode))
            {
                sql += " and ItemCode like @ItemCode ";
            }

            if (!string.IsNullOrEmpty(ItemName))
            {
                sql += " and ItemName like @ItemName ";
            }
            if (ItemTypeId != -1)
            {
                sql += " and ItemTypeId  = @ItemTypeId ";
            }


            var count = await Db.GetRecoreCountAsync(sql, new { ItemCode = $"%{ItemCode}%", ItemName = $"%{ItemName}%",ItemTypeId });
            return count;
        }

        public async Task<IEnumerable<ProductBom>> GetProductBombyIdAsync(string ItemId)
        {
            var list = await Db.QueryAsync<ProductBom>("select  * from ProductBom where ItemId = @ItemId", new { ItemId });
            return list;
        }

        public async Task<bool> UpdateAsync(Item model, List<ProductBom> list)
        {
            var result = await Db.UpdateIdentityTranAsync(model, list);
            return result;
        }
    }
}
